import wxValidate from 'wechat-validate'
Page({
  behaviors: [wxValidate],
  data: {
    // 当前时间的时间戳
    currentDate: +new Date(),
    houseLayerVisible: false,
    repairLayerVisible: false,
    dateLayerVisible: false,
    houseList: [],
    repairItem: [],
    attachment: [
      { url: '/repair_pkg/static/uploads/attachment.jpg' },
      { url: '/repair_pkg/static/uploads/attachment.jpg' },
    ],
    houseInfo: '',
    houseId: '',
    repairItemName: '',
    repairItemId: '',
    appointment: '',
    mobile: '',
    description: ''
  },
  rules: {
    houseId: [
      {required: true, message: '请选择报修房屋!'}
    ],
    repairItemId: [
      {required: true, message: '请选择维修的项目!'}
    ],
    mobile: [
      {required: true, message: '请填写手机号码!'},
      {pattern: /^1[3-8]\d{9}$/, message: '请填写正确的手机号码!'},
    ],
    appointment: [
      {required: true, message: '请选择预约日期!'}
    ],
    description: [
      {required: true, message: '请填写问题描述!'}
    ],
  },
  onLoad({ id }) {
    this.getHouseList()
    this.getRepairItem()
    if (id) {
      this.getRepairDetail(id)
    }
  },

  async getRepairDetail(id) {
    if (!id) return wx.utils.toast('参数有误!')
    // 调用接口
    const { code, data: repairDetail } = await wx.http.get('/repair/' + id)
    // 检测接口是否调用成功
    if (code !== 10000) return wx.utils.toast()
    // 渲染数据
    this.setData({ ...repairDetail })
  },

  async upload(e) {
    // console.log(e.detail.file.url)
    const {code, data} = await wx.http.upload('/upload', {
      name: 'file',
      filePath: e.detail.file.url
    })
    if (code !== 10000) return wx.utils.toast('图片上传失败')
    // console.log(data)
    const { attachment } = this.data
    attachment.push({ url: data.url })
    this.setData({ attachment })
  },

  async getRepairItem() {
    const { code, data } = await wx.http.get('/repairItem')
    if (code !== 10000) return
    this.setData({
      repairItem: data
    })
  },

  selectRepair(e) {
    this.setData({
      repairItemName: e.detail.name,
      repairItemId: e.detail.id
    })
  },

  async getHouseList() {
    const { code, data } = await wx.http.get('/house')
    if (code !== 10000) return
    // console.log(data)
    this.setData({
      houseList: data
    })
  },

  selectHouse(e) {
    // console.log(e.detail.name)
    // console.log(e.detail.id)
    this.setData({
      houseInfo: e.detail.name,
      houseId: e.detail.id
    })
  },

  openHouseLayer() {
    this.setData({ houseLayerVisible: true })
  },
  closeHouseLayer() {
    this.setData({ houseLayerVisible: false })
  },
  openRepairLayer() {
    this.setData({ repairLayerVisible: true })
  },
  closeRepairLayer() {
    this.setData({
      repairLayerVisible: false,
    })
  },

  openDateLayer() {
    this.setData({ dateLayerVisible: true })
  },
  closeDateLayer() {
    this.setData({ dateLayerVisible: false })
  },
  selectDate(e) {
    this.setData({
      appointment: wx.utils.formatDate(e.detail)
    })
    // 关闭 popup
    this.closeDateLayer()
  },
  async goList() {
    if (!this.validate()) return
    // 发请求给服务器
    const {
      id,
      houseId,
      repairItemId,
      mobile,
      appointment,
      description,
      attachment
    } = this.data
    const { code } = await wx.http.post('/repair', {
      id,
      houseId,
      repairItemId,
      mobile,
      appointment,
      description,
      attachment
    })
    if (code !== 10000) return wx.utils.toast('报修失败')
    // 成功后跳转到保修列表
    wx.redirectTo({
      url: '/repair_pkg/pages/list/index',
    })
  },
})
